package com.spring.sunflower.anchor

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseActivity
import com.spring.sunflower.bean.PhotoListBean
import com.spring.sunflower.common.CommonContract
import com.spring.sunflower.common.PhotoViewerActivity
import com.spring.sunflower.constant.Constant
import java.util.ArrayList
import java.util.HashMap

/**
 * Created by Kevin on 2021/9/13<br/>
 *
 * Describe:<br/>
 */
class PhotoListActivity : BaseActivity<PhotoListPresenter>(), CommonContract.PhotoListView {
    private var recyclerView: RecyclerView? = null
    private var mAdapter: PhotoListAdapter? = null
    private val mData: MutableList<PhotoListBean.DatasBean> = ArrayList()
    private var refreshLayout: SmartRefreshLayout? = null
    private var pageIndex = 1
    private var otherID: String? = null
    private var tempList: ArrayList<String> = ArrayList()
    override fun getLayoutID(): Int = R.layout.activity_photo_list

    override fun setPresenter(): PhotoListPresenter = PhotoListPresenter(this)

    override fun initView() {
        val intent = intent
        otherID = intent.getStringExtra("otherId")
        tvTitle.text = "TA的相册"
        recyclerView = findViewById(R.id.rvRecyclerView)
        refreshLayout = findViewById(R.id.refreshLayout)
        val layoutManager = GridLayoutManager(this, 3)
        recyclerView!!.setLayoutManager(layoutManager)
        mAdapter = PhotoListAdapter(R.layout.adapter_item_photo, mData)
        recyclerView!!.setAdapter(mAdapter)
        refreshLayout!!.setEnableRefresh(false)
        refreshLayout!!.setOnLoadMoreListener(OnLoadMoreListener {
            val map: MutableMap<String, String> = HashMap()
            map["memberId"] = otherID!!
            map["pageIndex"] = (++pageIndex).toString()
            mPresenter.queryPhoto(map)
        })
        val map: MutableMap<String, String> = HashMap()
        map["memberId"] = otherID!!
        map["pageIndex"] = pageIndex.toString()
        mPresenter.queryPhoto(map)

        mAdapter!!.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                tempList.clear()
                for (d in 0 until mData.size) {
                    tempList.add(mData[d].photoPath)
                }
                val intent = Intent(this@PhotoListActivity, PhotoViewerActivity::class.java)
                intent.putStringArrayListExtra(Constant.PHOTO_LIST_VIEWER, tempList);
                intent.putExtra(Constant.PHOTO_CURRENT_POSITION, position);
                startActivity(intent)
            }

        })
    }

    override fun querySuccess(data: MutableList<PhotoListBean.DatasBean>?) {
        if (pageIndex == 1) {
            if (!data.isNullOrEmpty()) {
                mAdapter!!.setList(data)
            }

        } else {
            mAdapter!!.addData(data!!)
        }
        if (refreshLayout!!.isLoading) {
            refreshLayout!!.finishLoadMore()
        }

    }

    override fun queryFailed() {
    }
}